#include "globalvar2.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
float g_temp = 0.0;
float g_humi = 0.0f;
float g_gas = 0.0f;
int g_door_alarm = 0;

void data_split(void)
{
    char temp_str[5];
    char humi_str[5];
    char gas_str[5];
    int door_alarm_str[1];
    char *endptr;

    for(int i = 0; i < 5; i++)
    {
        temp_str[i] = mq_buf[i];
    }
    // temp_str[5] = '\0';
    // g_temp = atof(temp_str);
    g_temp = strtof(temp_str, &endptr);
    printf("var2 temp ======== %.2f\n", g_temp);

    humi_str[0] = mq_buf[5];
    humi_str[1] = mq_buf[6];
    humi_str[2] = mq_buf[7];
    humi_str[3] = mq_buf[8];
    humi_str[4] = mq_buf[9];
    // g_humi = atof(humi_str);
    g_humi = strtof(humi_str, &endptr);
    printf("var2 humi ======== %.2f\n", g_humi);

    gas_str[0] = mq_buf[10];
    gas_str[1] = mq_buf[11];
    gas_str[2] = mq_buf[12];
    gas_str[3] = mq_buf[13];
    gas_str[4] = mq_buf[14];
    g_gas = strtof(gas_str, &endptr);
    printf("var2 gas ======== %.2f\n", g_gas);

    door_alarm_str[0] = mq_buf[15];
    g_door_alarm = atoi(door_alarm_str);
    printf("var2 g_door_alarm ======== %.2f\n", g_door_alarm);
}

void Var2_task(void)
{
    while (1) 
    {
        data_split();
        usleep (1 * 1000 * 1000);
        printf("mq_buf::::::::::::::::::::::::%s\n", mq_buf);
    }
}

void Var2_entry(void)
{
    usleep(3*1000*1000); //延时3S
    
    osThreadAttr_t attr;  //任务属性的结构体

    attr.name = "Var2_task";    //任务名称
    attr.stack_size = 4 * 1024;  //任务的内存大小
    attr.priority = 25;          //任务的优先级

    //创建任务
    if (osThreadNew((osThreadFunc_t)Var2_task, NULL, &attr) == NULL) 
    {
        //任务创建失败
        // PRINTF_DEBUG(2 , "  Failed to create Alarm_task!\r\n");
        printf("  Failed to create Alarm_task!\r\n");
    }
    //任务创建成功
    // PRINTF_DEBUG(0 , "  Succeed to create Alarm_task!\r\n");
    printf("  Succeed to create Alarm_task!\r\n");
}

/** 让系统运行指定的函数 **/
SYS_RUN(Var2_entry);